using System;
using System.Collections.Generic;
using UnityEngine;

public class IOSInAppPurchaseManager : ISN_Singleton<IOSInAppPurchaseManager>
{
	public const string APPLE_VERIFICATION_SERVER = "https://buy.itunes.apple.com/verifyReceipt";

	public const string SANDBOX_VERIFICATION_SERVER = "https://sandbox.itunes.apple.com/verifyReceipt";

	public const string TRANSACTION_COMPLETE = "transaction_complete";

	public const string RESTORE_TRANSACTION_FAILED = "restore_transaction_failed";

	public const string RESTORE_TRANSACTION_COMPLETE = "restore_transaction_complete";

	public const string VERIFICATION_RESPONSE = "verification_response";

	public const string STORE_KIT_INITIALIZED = "store_kit_initialized";

	public const string STORE_KIT_INITI_FAILED = "store_kit_init_failed";

	public Action<IOSStoreKitResponse> OnTransactionComplete = delegate
	{
	};

	public Action<IOSStoreKitRestoreResponce> OnRestoreComplete = delegate
	{
	};

	public Action<ISN_Result> OnStoreKitInitComplete = delegate
	{
	};

	public Action<bool> OnPurchasesStateSettingsLoaded = delegate
	{
	};

	public Action<IOSStoreKitVerificationResponse> OnVerificationComplete = delegate
	{
	};

	private bool _IsStoreLoaded;

	private bool _IsWaitingLoadResult;

	private bool _IsInAppPurchasesEnabled = true;

	private static int _nextId = 1;

	private List<string> _productsIds = new List<string>();

	private List<IOSProductTemplate> _products = new List<IOSProductTemplate>();

	private Dictionary<int, IOSStoreProductView> _productsView = new Dictionary<int, IOSStoreProductView>();

	private static IOSInAppPurchaseManager _instance;

	private static string lastPurchasedProduct;

	public List<IOSProductTemplate> products
	{
		get
		{
			return _products;
		}
	}

	public bool IsStoreLoaded
	{
		get
		{
			return _IsStoreLoaded;
		}
	}

	public bool IsInAppPurchasesEnabled
	{
		get
		{
			return _IsInAppPurchasesEnabled;
		}
	}

	public bool IsWaitingLoadResult
	{
		get
		{
			return _IsWaitingLoadResult;
		}
	}

	public static int nextId
	{
		get
		{
			_nextId++;
			return _nextId;
		}
	}

	private void Awake()
	{
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
	}

	public void loadStore()
	{
		if (_IsStoreLoaded)
		{
			Invoke("FireSuccessInitEvent", 1f);
		}
		else
		{
			if (_IsWaitingLoadResult)
			{
				return;
			}
			_IsWaitingLoadResult = true;
			foreach (string inAppProduct in IOSNativeSettings.Instance.InAppProducts)
			{
				addProductId(inAppProduct);
			}
			string text = string.Empty;
			int count = _productsIds.Count;
			for (int i = 0; i < count; i++)
			{
				if (i != 0)
				{
					text += ",";
				}
				text += _productsIds[i];
			}
			IOSNativeMarketBridge.loadStore(text);
		}
	}

	public void RequestInAppSettingState()
	{
		IOSNativeMarketBridge.ISN_RequestInAppSettingState();
	}

	public void buyProduct(string productId)
	{
		if (!_IsStoreLoaded)
		{
			if (!IOSNativeSettings.Instance.DisablePluginLogs)
			{
				BCWDebug.LogWarning("buyProduct shouldn't be called before StoreKit is initialized");
			}
			SendTransactionFailEvent(productId, "StoreKit not yet initialized", IOSTransactionErrorCode.SKErrorPaymentNotAllowed);
		}
		else
		{
			IOSNativeMarketBridge.buyProduct(productId);
		}
	}

	public void addProductId(string productId)
	{
		if (!_productsIds.Contains(productId))
		{
			_productsIds.Add(productId);
		}
	}

	public IOSProductTemplate GetProductById(string id)
	{
		foreach (IOSProductTemplate product in products)
		{
			if (product.id.Equals(id))
			{
				return product;
			}
		}
		return null;
	}

	public void restorePurchases()
	{
		if (!_IsStoreLoaded)
		{
			IOSStoreKitError iOSStoreKitError = new IOSStoreKitError();
			iOSStoreKitError.code = IOSTransactionErrorCode.SKErrorPaymentServiceNotInitialized;
			iOSStoreKitError.description = "Store Kit Initilizations required";
			IOSStoreKitRestoreResponce iOSStoreKitRestoreResponce = new IOSStoreKitRestoreResponce(false, iOSStoreKitError);
			OnRestoreComplete(iOSStoreKitRestoreResponce);
			dispatch("restore_transaction_failed", iOSStoreKitRestoreResponce);
		}
		else
		{
			IOSNativeMarketBridge.restorePurchases();
		}
	}

	[Obsolete("verifyLastPurchase is deprecated, please use VerifyLastPurchase instead.")]
	public void verifyLastPurchase(string url)
	{
		VerifyLastPurchase(url);
	}

	public void VerifyLastPurchase(string url)
	{
		IOSNativeMarketBridge.verifyLastPurchase(url);
	}

	public void RegisterProductView(IOSStoreProductView view)
	{
		view.SetId(nextId);
		_productsView.Add(view.id, view);
	}

	private void onStoreKitStart(string data)
	{
		int num = Convert.ToInt32(data);
		if (num == 1)
		{
			_IsInAppPurchasesEnabled = true;
		}
		else
		{
			_IsInAppPurchasesEnabled = false;
		}
		OnPurchasesStateSettingsLoaded(_IsInAppPurchasesEnabled);
	}

	private void OnStoreKitInitFailed(string data)
	{
		string[] array = data.Split("|"[0]);
		ISN_Error iSN_Error = new ISN_Error();
		iSN_Error.code = Convert.ToInt32(array[0]);
		iSN_Error.description = array[1];
		_IsStoreLoaded = false;
		_IsWaitingLoadResult = false;
		ISN_Result iSN_Result = new ISN_Result(false);
		iSN_Result.error = iSN_Error;
		dispatch("store_kit_init_failed", iSN_Result);
		OnStoreKitInitComplete(iSN_Result);
		if (!IOSNativeSettings.Instance.DisablePluginLogs)
		{
			BCWDebug.Log("STORE_KIT_INIT_FAILED Error: " + iSN_Error.description);
		}
	}

	private void onStoreDataReceived(string data)
	{
		if (data.Equals(string.Empty))
		{
			BCWDebug.Log("InAppPurchaseManager, no products avaiable: " + _products.Count);
			ISN_Result iSN_Result = new ISN_Result(true);
			dispatch("store_kit_initialized", iSN_Result);
			OnStoreKitInitComplete(iSN_Result);
			return;
		}
		string[] array = data.Split("|"[0]);
		for (int i = 0; i < array.Length; i += 7)
		{
			IOSProductTemplate iOSProductTemplate = new IOSProductTemplate();
			iOSProductTemplate.id = array[i];
			iOSProductTemplate.title = array[i + 1];
			iOSProductTemplate.description = array[i + 2];
			iOSProductTemplate.localizedPrice = array[i + 3];
			iOSProductTemplate.price = array[i + 4];
			iOSProductTemplate.currencyCode = array[i + 5];
			iOSProductTemplate.currencySymbol = array[i + 6];
			_products.Add(iOSProductTemplate);
		}
		BCWDebug.Log("InAppPurchaseManager, total products loaded: " + _products.Count);
		FireSuccessInitEvent();
	}

	private void onProductBought(string array)
	{
		string[] array2 = array.Split("|"[0]);
		bool isRestored = false;
		if (array2[1].Equals("0"))
		{
			isRestored = true;
		}
		FireProductBoughtEvent(array2[0], array2[2], array2[3], isRestored);
	}

	private void onProductStateDeferred(string productIdentifier)
	{
		IOSStoreKitResponse iOSStoreKitResponse = new IOSStoreKitResponse();
		iOSStoreKitResponse.productIdentifier = productIdentifier;
		iOSStoreKitResponse.state = InAppPurchaseState.Deferred;
		dispatch("transaction_complete", iOSStoreKitResponse);
		OnTransactionComplete(iOSStoreKitResponse);
	}

	private void onTransactionFailed(string array)
	{
		string[] array2 = array.Split("|"[0]);
		SendTransactionFailEvent(array2[0], array2[1], (IOSTransactionErrorCode)Convert.ToInt32(array2[2]));
	}

	private void onVerificationResult(string array)
	{
		string[] array2 = array.Split("|"[0]);
		IOSStoreKitVerificationResponse iOSStoreKitVerificationResponse = new IOSStoreKitVerificationResponse();
		iOSStoreKitVerificationResponse.status = Convert.ToInt32(array2[0]);
		iOSStoreKitVerificationResponse.originalJSON = array2[1];
		iOSStoreKitVerificationResponse.receipt = array2[2];
		iOSStoreKitVerificationResponse.productIdentifier = lastPurchasedProduct;
		dispatch("verification_response", iOSStoreKitVerificationResponse);
		OnVerificationComplete(iOSStoreKitVerificationResponse);
	}

	public void onRestoreTransactionFailed(string array)
	{
		string[] array2 = array.Split("|"[0]);
		IOSStoreKitError iOSStoreKitError = new IOSStoreKitError();
		iOSStoreKitError.code = (IOSTransactionErrorCode)Convert.ToInt32(array2[0]);
		iOSStoreKitError.description = array2[1];
		IOSStoreKitRestoreResponce iOSStoreKitRestoreResponce = new IOSStoreKitRestoreResponce(false, iOSStoreKitError);
		dispatch("restore_transaction_failed", iOSStoreKitRestoreResponce);
		OnRestoreComplete(iOSStoreKitRestoreResponce);
	}

	public void onRestoreTransactionComplete(string array)
	{
		FireRestoreCompleteEvent();
	}

	private void OnProductViewLoaded(string viewId)
	{
		int key = Convert.ToInt32(viewId);
		if (_productsView.ContainsKey(key))
		{
			_productsView[key].OnContentLoaded();
		}
	}

	private void OnProductViewLoadedFailed(string viewId)
	{
		int key = Convert.ToInt32(viewId);
		if (_productsView.ContainsKey(key))
		{
			_productsView[key].OnContentLoadFailed();
		}
	}

	private void OnProductViewDismissed(string viewId)
	{
		int key = Convert.ToInt32(viewId);
		if (_productsView.ContainsKey(key))
		{
			_productsView[key].OnProductViewDismissed();
		}
	}

	private void FireSuccessInitEvent()
	{
		_IsStoreLoaded = true;
		_IsWaitingLoadResult = false;
		ISN_Result obj = new ISN_Result(true);
		dispatch("store_kit_initialized");
		OnStoreKitInitComplete(obj);
	}

	private void FireRestoreCompleteEvent()
	{
		IOSStoreKitRestoreResponce iOSStoreKitRestoreResponce = new IOSStoreKitRestoreResponce(true, null);
		dispatch("restore_transaction_complete", iOSStoreKitRestoreResponce);
		OnRestoreComplete(iOSStoreKitRestoreResponce);
	}

	private void FireProductBoughtEvent(string productIdentifier, string receipt, string transactionIdentifier, bool IsRestored)
	{
		IOSStoreKitResponse iOSStoreKitResponse = new IOSStoreKitResponse();
		iOSStoreKitResponse.productIdentifier = productIdentifier;
		iOSStoreKitResponse.receipt = receipt;
		iOSStoreKitResponse.transactionIdentifier = transactionIdentifier;
		if (IsRestored)
		{
			iOSStoreKitResponse.state = InAppPurchaseState.Restored;
		}
		else
		{
			iOSStoreKitResponse.state = InAppPurchaseState.Purchased;
		}
		lastPurchasedProduct = iOSStoreKitResponse.productIdentifier;
		dispatch("transaction_complete", iOSStoreKitResponse);
		OnTransactionComplete(iOSStoreKitResponse);
	}

	private void SendTransactionFailEvent(string productIdentifier, string errorDescribtion, IOSTransactionErrorCode errorCode)
	{
		IOSStoreKitResponse iOSStoreKitResponse = new IOSStoreKitResponse();
		iOSStoreKitResponse.productIdentifier = productIdentifier;
		iOSStoreKitResponse.state = InAppPurchaseState.Failed;
		iOSStoreKitResponse.error = new IOSStoreKitError();
		iOSStoreKitResponse.error.description = errorDescribtion;
		iOSStoreKitResponse.error.code = errorCode;
		dispatch("transaction_complete", iOSStoreKitResponse);
		OnTransactionComplete(iOSStoreKitResponse);
	}
}
